Synchronization is a mechanism used when multithreading in Java to ensure that only one thread executes a given block of code at a time. This is
done to avoid bugs that can occur when multiple threads share a given state and try to manipulate simultaneously.
Object serialization is not thread-safe by default. In a multithreaded environment, one option is to mark writeObject
with
synchronized
to improve thread safety. It is highly suspicious, however, if writeObject
is the only
synchronized
method in a class. It may indicate that serialization is not required, as multithreading is not used. Alternatively, it
could also suggest that other methods in the same class have been forgotten to be made thread-safe.